package com.xiaomi.baidumap.github;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaomi.baidumap.R;
import com.xiaomi.baidumap.common.ActivityUtils;
import com.xiaomi.baidumap.model.Repo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RepoInfoActivity extends AppCompatActivity implements RepoInfoPresenter.RepoInfoView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ivIcon)
    ImageView ivIcon;
    @BindView(R.id.tvRepoName)
    TextView tvRepoName;
    @BindView(R.id.tvRepoInfo) TextView tvRepoInfo;
    @BindView(R.id.tvRepoStars) TextView tvRepoStars;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;


    private Repo repo;
    private static final String KEY_REPO = "key_repo";
    private ActivityUtils mActivityUtils;
    private RepoInfoPresenter mInfoPresenter;

    //防止数据的丢失，用这种方法传递数据和跳转页面
    public static void open(Context context,Repo repo){

        Intent intent = new Intent(context,RepoInfoActivity.class);
        intent.putExtra(KEY_REPO,repo);
        //开始跳转
        context.startActivity(intent);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityUtils = new ActivityUtils(this);
        //设置布局的时候就会触发或者调用onContentChanged()
        setContentView(R.layout.activity_repo_info);

        mInfoPresenter = new RepoInfoPresenter(this);
        mInfoPresenter.getReadme(repo);

    }

    //数据的处理和展示

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        //传递之后获得数据
        repo = (Repo) getIntent().getSerializableExtra(KEY_REPO);
        //这个要多注意一下
        ButterKnife.bind(this);
        //设置toolbar
        setSupportActionBar(toolbar);
        //设置toolbar返回的按键
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        //设置主题
        getSupportActionBar().setTitle(repo.getName());
        //设置名字
        tvRepoName.setText(repo.getFullName());
        //设置仓库的描述
        tvRepoInfo.setText(repo.getDescription());
        //占位符
        tvRepoStars.setText(String.format("start: %d   fork: %d ", repo.getStargazersCount(), repo.getForksCount()));
        //找到图片显示到ivIcon上面
        ImageLoader.getInstance().displayImage(repo.getOwner().getAvatar(),ivIcon);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showMessager(String msg) {
        mActivityUtils.showToast(msg);
    }

    @Override
    public void setData(String htmlContent) {
        //webview加载数据
        //加载html字符创数据
        webView.loadData(htmlContent,"text/html","UTF-8");
    }
}
